package com.wuxiaofan.secondkill.service;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuxiaofan.secondkill.client.Idgenerator;
import com.wuxiaofan.secondkill.entity.User;
import com.wuxiaofan.secondkill.entity.custom.UserCustom;
import com.wuxiaofan.secondkill.mapper.UserMapper;
import com.wuxiaofan.secondkill.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper tUserMapper;
    @Resource
    private Idgenerator idgenerator;

    public User findById(Long id){
        return tUserMapper.selectById(id);
    }

    public UserCustom findCountByMap(Map paraMap){
        return tUserMapper.findCountByMap(paraMap);
    }

    public UserCustom findGroupByMap(Map paraMap){
        return tUserMapper.findGroupByMap(paraMap);
    }

    public List<UserCustom> findByMap(Map paraMap){
        return tUserMapper.findByMap(paraMap);
    }

    public Page<UserCustom> findByMap(Map paraMap, Page page){
        return page.setRecords(tUserMapper.findByMap(paraMap,page));
    }


    public void updateMapById(Map paraMap){
        tUserMapper.updateMapById(paraMap);
    }
    public void delById(String id){
        tUserMapper.deleteById(id);
    }

    public User save(User tUser){
        tUser.setId(idgenerator.getId());
        String salt = UUID.fastUUID().toString();
        tUser.setPasswd(Md5Util.encode(tUser.getPasswd(),salt));
        tUserMapper.insert(tUser);
        return tUser;
    }

}
